package com.ksyun.registry.controller;

import com.ksyun.registry.domain.dto.DiscoveryResponce;
import com.ksyun.registry.domain.dto.HeartBeat;
import com.ksyun.registry.domain.dto.Response;
import com.ksyun.registry.domain.dto.ServiceInstance;
import com.ksyun.registry.service.RegistryService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class RegistryController {
    private final RegistryService registryService;

    //服务注册
    @PostMapping("/register")
    public ResponseEntity<Response> register(@RequestBody ServiceInstance serviceInstance){
        //存储对象
        Response response=registryService.register(serviceInstance);
        return ResponseEntity.ok(response);
    }

    @PostMapping("/unregister")
    public ResponseEntity<Response> unregister(@RequestBody ServiceInstance serviceInstance){
        //注销服务实例
        Response response =  registryService.unregister(serviceInstance);
        return ResponseEntity.ok(response);
    }

    @PostMapping("/heartbeat")
    public ResponseEntity<Response> heartbeat(@RequestBody HeartBeat heartBeat){
        //心跳机制，调用的服务
        Response response= registryService.heartbeat(heartBeat);
        return ResponseEntity.ok(response);
    }

    @GetMapping("/discovery")
    public ResponseEntity<List<ServiceInstance>> discovery(@RequestParam(value = "name",required = false) String name){
        List<ServiceInstance> response=registryService.discovery(name);
        return ResponseEntity.ok(response);
    }
}
